package com.supermap.log.service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dubbo.DataDubboConcumerService;
import com.supermap.tool.*;
import com.supermap.util.SQLFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.supermap.log.entity.UserOperationLog;
import com.supermap.user.service.UserService;


/**
 * @author yuyao
 * @ClassName: LogService
 * @Description: 操作日记记录
 * @date 2018年3月20日 上午9:40:35
 */
@Service
public class LogService extends Thread {

    @Autowired
    TableTool tableTool;
    @Resource
    DataDubboConcumerService dataDubboConcumerService;

    /**
     * @param log 设定文件
     * @return void    返回类型
     * @throws
     * @Title: saveLog
     * @Description: 操作记录
     */
    public void saveLog(final UserOperationLog log) {
        new Thread() {
            @Override
            public void run() {
                MysqlEntity st = new MysqlEntity();
                st.setTableName("user_operation_log");
                JSONObject uv = (JSONObject) JSON.toJSON(log);
                st.setUpdateValue(uv);
                st.setUpdate(false);
                String fastInsertSql = SQLFactory.getFastInsertSql(st);
                dataDubboConcumerService.getDataWithInsert(fastInsertSql);
            }
        }.start();
    }

    /**
     * @param @param  table
     * @param @param  log
     * @param @return 设定文件
     * @return Table    返回类型
     * @throws
     * @Title: getLogList
     * @Description: 获取日记列表
     */
    public TableVoo getLogList(TableVoo table, UserOperationLog log) {
        StringBuilder sql = new StringBuilder();
        StringBuilder con = new StringBuilder();
        con.append("SELECT COUNT(1) count FROM (");
        sql.append("SELECT * FROM user_operation_log ");
        con.append(sql);
        sql.append("ORDER BY operationtime DESC ");
        con.append(") a");

        return tableTool.getTable(table, sql.toString(), con.toString(), "user_operation_log");
    }

    /**
     * @param @param  request
     * @param @return 设定文件
     * @return UserOperationLog    返回类型
     * @throws
     * @Title: getLog
     * @Description: 获取用户信息
     */
    public UserOperationLog getLog(JSONObject user, HttpServletRequest reques) {
        UserOperationLog log = new UserOperationLog();
        log.setLogIP(StringTool.getIpAddress(reques));
        if (StringTool.isNotNull(user.getString("areaCode"))) {
            log.setAddress(user.getString("areaCode"));
        }
        /*log.setAddressname(user.getString(""));*/
        if (StringTool.isNotNull(user.getString("id"))) {
            log.setUserId(user.getString("id"));
        }
        if (StringTool.isNotNull(user.getString("loginName"))) {
            log.setUserName(user.getString("loginName"));
        }
        return log;
    }

}
